package com.nirvana.utils;

import com.nirvana.config.PluginConfig;

/**
 * @author: create by skinjay
 * @date:2021/6/23
 */
public class FactoryCodeUtil {
    public static String generateFactoryCode(String entityName, String tableName) {
        StringBuilder result = new StringBuilder();
        /* 所属包 */
        result.append("package " + PluginConfig.getInstance().getFactoryPackage())
                .append(";\n\n");

        /* 导入框架内部引用 */
        result.append(generateImport(entityName)).append("\n");

        /* 生成名 */
        result.append(generateFactoryName(entityName, tableName))
                .append("\n");
        result.append("{\n");


        result.append(generateGetEntityByUserID(entityName)).append("\n");


        result.append("}\n");
        return result.toString();
    }

    private static String generateFactoryName(String entityName,
                                              String tableName) {
        StringBuilder result = new StringBuilder();
        result.append("public class "
                + CommonUtil.CreateFactoryName(entityName)
                + " implements IDaoFactory");
        return result.toString();
    }

    private static String generateGetEntityByUserID(String entityName) {
        StringBuilder result = new StringBuilder();
        result.append("\t").append("private static final I").append(CommonUtil.CreateEntityName(entityName)).append("Dao dao = new ").append(CommonUtil.CreateEntityName(entityName)).append("DaoImpl(gameDbHelper);").append("\n");

        result.append("\t").append("public static I").append(CommonUtil.CreateEntityName(entityName)).append("Dao getDao() { return dao; }");
        return result.toString();
    }

    private static String generateImport(String entityName) {
        StringBuilder result = new StringBuilder();
        result.append("import " + PluginConfig.getInstance().getDaoImplPackage() + "."
                + CommonUtil.CreateEntityName(entityName) + "DaoImpl;\n\n");
        result.append("import " + PluginConfig.getInstance().getDaoPackage()).append(".I").append(CommonUtil.CreateEntityName(entityName)).append("Dao;\n\n");
        return result.toString();

    }
}
